import math

class StateTra():
    def __init__(self,Sce,RAction,BAction):
        self.sce=Sce
        self.analy_location(RAction[0])  # 分析机动动作的影响
        self.analy_attack(RAction[1], 0)  # 分析攻击动作的影响,0为我方,1为AI
        self.analy_attack(BAction, 1)  # 分析攻击动作的影响,0为我方,1为AI
        self.analy_result()

    def analy_location(self,flight):  # 分析动作会对环境哪些方面产生影响，进行拆分
        a=0
        for i in self.sce.Red:
            i.Fly_angle(flight[a][0],flight[a][1],self.sce.dt)
            a+=1

    def analy_attack(self,attack,D_W):  # TODO　（ｚｒｃ）　接收到的参数为[[0，1，0，1]...[...]]4*4的矩阵
        # 添加攻击动作
        a=-1
        for att in attack:
            a+=1
            if D_W == 0:    # 我方
                for i in range(len(att)):
                    if att[i] == 1 and self.friends[a].Attack(self.enemies[i].Position):
                        missile = Missile(self.count_round,0,self.friends[a].Position,self.enemies[i].Position)
                        self.att_target_f[a][i].append(missile)


            else:   # 敌方
                for i in range(len(att)):
                    if att[i] == 1 :
                        self.enemies[a].cd_reset()
                        missile=Missile(self.count_round,1,self.enemies[a].Position,self.friends[i].Position)
                        self.att_target_e[a][i].append(missile)

        #DD的动作：
        for i in range(4):
            for j in range(4):
                for k in self.att_target_f[i][j] :
                    if k.activity==1 and k.D_W==D_W:
                        k.fly(self.dt)
                for k in self.att_target_e[i][j] :
                    if k.activity==1 and k.D_W==D_W:
                        k.fly(self.dt)

    def analy_result(self):  # 对环境进行更改，返回observation_和done
        done=True   # True意为未结束
        reward_done=0
        self.attack()

        livelist = [[f.live for f in self.friends], [e.live for e in self.enemies]]

        if (np.array(livelist[0])==0).all() :
            done=False
            # 如果wo方全灭，直接结算wo全部导弹,再计算回报
            for i in self.att_target_f:
                for j in i:
                    for m in j:
                        m.att_countdown=0
            self.attack()
            reward_done = self.reward()

        elif (np.array(livelist[1])==0).all():
            done = False
            # 如果di方全灭，直接结算di全部导弹,再计算回报
            for i in self.att_target_e:
                for j in i:
                    for m in j:
                        m.att_countdown=0
            self.attack()
            reward_done = self.reward()
        elif self.count_round>self.rou:
            done=False
            reward_done=self.reward()
        observation=self.get_observation()
